package util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.apache.commons.lang.SystemUtils;

public class YBCharsetTransfer {
	/**
	 * @see Convert value from 'fromCharset' to 'toCharset', e.g. from 'ASCII'
	 *      to 'UTF-8'
	 * @param value
	 * @param fromCharset
	 * @param toCharset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String transfer(String value, String fromCharset,
			String toCharset) throws UnsupportedEncodingException {
		String s = URLEncoder.encode(value, fromCharset);
		return URLDecoder.decode(s, toCharset);
	}

	/**
	 * @see Use System default charset as from Charset
	 * @param value
	 * @param toCharset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String transfer(String value, String toCharset)
			throws UnsupportedEncodingException {
		String s = URLEncoder
				.encode(value, Charset.defaultCharset().toString());
		return URLDecoder.decode(s, toCharset);
	}

	/**
	 * @see Use System default charset as from Charset, UTF-8 as to charset
	 * @param value
	 * @param toCharset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String transfer(String value)
			throws UnsupportedEncodingException {
		String s = URLEncoder
				.encode(value, Charset.defaultCharset().toString());
		return URLDecoder.decode(s, "UTF-8");
	}

	public static final String QBchange(String QJstr) {
		String outStr = "";
		String Tstr = "";
		byte[] b = null;

		for (int i = 0; i < QJstr.length(); i++) {
			try {
				Tstr = QJstr.substring(i, i + 1);
				b = Tstr.getBytes("unicode");
			} catch (java.io.UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (b[3] != -1) {
				b[2] = (byte) (b[2] + 32);
				b[3] = 0;
				try {
					outStr = outStr + new String(b, "unicode");
				} catch (java.io.UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			} else
				outStr = outStr + Tstr;
		}
		return outStr;
	}


	public static void main(String[] args) throws UnsupportedEncodingException {

		String value = "This hydrophilic Water Shoe by DBX� is the perfect footwear for any aquatic activity. Consisting of neoprene and mesh, the upper allows for breathable, fast drying comfort, while the flexible rubber bottom provides underfoot protection.";
		System.out.println(System.getProperty(SystemUtils.FILE_ENCODING));
		System.out.println(transfer(value, "UTF-8","ISO-8859-1"));
//		System.out.println(input.replace("\u201C", "\""));

	}

}
